package com.vic.dead_letter_queue.ttl;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.vic.dead_letter_queue.Consts;
import com.vic.utils.RabbitmqUtils;

import java.nio.charset.StandardCharsets;

/**
 * 生产者
 */
public class Producer {

    // 队列名称
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitmqUtils.getChannel();

        // 声明一个 direct 类型的交换机
        channel.exchangeDeclare(Consts.NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);

        // 消息发送 10 s 之后，如果没有消费者进行消费，那么该消息就称为死信，它就会进入死信队列中
        AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().expiration("10000").build();

        // 待发送的消息
        String message = "harriet";
        for (int i = 1; i <= 5; i++) {
            channel.basicPublish(Consts.NORMAL_EXCHANGE, Consts.NORMAL_ROUTING_KEY, properties, (message + i).getBytes(StandardCharsets.UTF_8));
        }

        System.out.println("消息发送完毕");
    }

}
